<?
	include "include/config.inc";
	$page["title"] = "Availability report";
	$page["file"] = "report2.html";
	show_header($page["title"],0);
?>

<?
	if(!isset($yes))
	{
		echo "<center>";
		echo "<a href=\"report2.html?yes=1\">";
		echo "Click here to run report";
		echo "</a>";
		echo "</center>";
		show_footer();
		exit;
	}

?>

<?
	$stats=get_stats();
	$txt="AVAILABILITY REPORT";
	$txt=$txt."<br>"."(From:".date("y/m/d H:i:s",$stats["history_min_clock"])." till:".date("y/m/d H:i:s",time()).")";
	show_table_header($txt);
	echo "<br>";
?>

<?

	$result=DBselect("select h.hostid,h.host,t.triggerid,t.expression,t.description,t.istrue from triggers t,hosts h,items i,functions f where f.itemid=i.itemid and h.hostid=i.hostid and t.istrue!=2 and t.triggerid=f.triggerid order by h.host,t.description");
	$lasthost="";
	$col=0;
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$hostid_=DBget_field($result,$i,0);
		$host=DBget_field($result,$i,1);
		if($lasthost!=$host)
		{
			if($lasthost!="")
			{
				echo "</TABLE><BR>";
			}
			show_table_header("$host");
			echo "<TABLE BORDER=0 COLS=3 WIDTH=\"100%\" BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=3>";
			echo "<TR>";
			echo "<TD><B>Description</B></TD>";
			echo "<TD><B>Expression</B></TD>";
			echo "<TD WIDTH=\"5%\"><B>True (%)</B></TD>";
			echo "<TD WIDTH=\"5%\"><B>False (%)</B></TD>";
			echo "</TR>\n";
		}
		$lasthost=$host;

	        if($col==1)
	        {
	                echo "<TR BGCOLOR=#DDDDDD>";
	                $col=0;
	        } else
	        {
	                echo "<TR BGCOLOR=#EEEEEE>";
	                $col=1;
	        }
		$description=DBget_field($result,$i,4);
		$triggerid_=DBget_field($result,$i,2);
		$expression=DBget_field($result,$i,3);
		$istrue=DBget_field($result,$i,5);
		echo "<TD>$description</TD>";
		$description=rawurlencode($description);

		echo "<TD>".explode_exp($expression,1)."</TD>";
		$availability=calculate_availability($triggerid_);
		echo "<TD>";
		printf("%.4f%%",$availability["true"]);
		echo "</TD>";
		echo "<TD>";
		printf("%.4f%%",$availability["false"]);
		echo "</TD>";
		echo "</TR>\n";
	}
	echo "</table>\n";
?>

<?
	show_footer();
?>
